<!--  -->
<template>
  <div class="app">
    <top-tit :tit="'个人中心'"></top-tit>
    <div class="head_icon">
      <div class="container">
        <div class="head_icon_box">
          <img :src="user_icon" alt="" />
          <span>点击头像更换</span>
        </div>
        <input type="file" name="" id="" @change="uploadAvatar" accept="image/*" />
      </div>
    </div>

    <div class="tit">
      <div class="container">账户信息</div>
    </div>

    <div class="main">
      <div class="div_item">
        <div class="container">
          <div class="value_name">昵称</div>
          <div class="user_name">
            <span v-text="user_name"></span>
            <span class="arrow_icon"></span>
          </div>
        </div>
      </div>
      <div class="div_item">
        <div class="container">
          <div class="value_name">绑定手机号码</div>
          <div class="user_name">
            <span v-text="phone"></span>
            <span class="arrow_icon"></span>
          </div>
        </div>
      </div>
      <div class="div_item">
        <div class="container">
          <div class="value_name">修改密码</div>
          <div class="user_name">
            <span>设置密码</span>
            <span class="arrow_icon">
                <!-- <img src="@/assets/img/next.png" alt=""> -->
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import topTit from "@/components/topTit";
import { Toast } from "vant";
export default {
  name: "userInfo",
  //import引入的组件需要注入到对象中才能使用
  components: {
    topTit,
  },
  data() {
    //这里存放数据
    return {
      user_name: "",
      user_icon: "",
      phone: "",
      token: localStorage.getItem("h5-token"),
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    uploadAvatar(e) {
      let file = e.target.files;
      // console.log(file[0]);
      if (!file[0].type.match(/.jpg|.png|.jpeg|.gif/i)) {
        Toast.fail("图片格式错误,请上传png/jpg/jpeg/gif格式的图片");
        return;
      }
      if (file[0].size > 2048000) {
        Toast.fail("图片大小不超过2M");
        return;
      }
      let formData = new FormData();
      formData.append("file", file[0]);
      // console.log(formData);
      this.$axios
        .post("http://1.14.239.98/api/v2/member/detail/avatar", formData)
        .then((res) => {
          // console.log(res);
          if (res.code === 0) {
            Toast.success("change succes");
            return "http://1.14.239.98/api/v2/member/detail";
          }
        })
        .then((url) => {
          this.$axios.get(url).then((res) => {
            // console.log(res);
            this.user_icon = res.data.avatar;
          });
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  async created() {
    // console.log("test1");
    if (this.token) {
      await this.$axios.get("http://1.14.239.98/api/v2/member/detail").then((res) => {
        // console.log(res);
        this.user_name = res.data.nick_name;
        this.user_icon = res.data.avatar;
        this.phone = res.data.mobile.slice(0, 3) + "****" + res.data.mobile.slice(7, 11);
        // console.log(this.phone);
      });
    }
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped lang="less">
div.app {
  div.head_icon {
    border-top: 1px solid #f1f2f6;
    padding-top: 30px;
    margin-bottom: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    div.container {
      width: 100%;
      height: 1.84rem;
      display: flex;
      // flex-direction: column;
      // justify-content: center;
      // align-items: center;
      position: relative;
      div.head_icon_box {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        margin: 0 auto;
        img {
          width: 1.28rem;
          height: 1.28rem;
          border-radius: 50%;
        }
        span {
          color: #666;
        }
      }

      input {
        position: absolute;
        width: 1.28rem;
        height: 1.28rem;
        left: calc(50% - 1.28rem / 2);
        opacity: 0;
      }
    }
  }

  div.tit {
    width: 100%;
    margin-bottom: 0.4rem;
    div.container {
      width: 6.9rem;
      color: #333;
      font-size: 0.32rem;
      font-weight: 600;
      text-align: start;
    }
  }

  div.main {
    margin: 0.3rem 0;
    background-color: #fff;
    .div_item {
      width: 100%;
      border-bottom: 1px solid rgba(0, 0, 0, 0.03);
      height: 1.06rem;

      div.container {
        width: 6.9rem;
        height: 1.06rem;
        text-align: start;
        // line-height: 1.06rem;
        display: flex;
        justify-content: space-between;
        div.value_name {
          font-size: 0.32rem;
          color: #333;
        }

        div.user_name {
          span {
            color: #666;
            font-size: 0.28rem;
          }

          span.arrow_icon {
            display: inline-block;
            width: .24rem;
            height: .24rem;
            margin-left:15px;
            // background-image: 'url(' + require('@/assets/img/next.png') +')';
            background-image: url('../../assets/img/next.png');
            background-size: .24rem .24rem;
          }
        }
      }
    }
  }
}
</style>
